"""
@date: 2023/7/11
@author: North
@desc: test

"""
import csv

import requests

"""
通过ip请求淘宝的一个接口获取到省份和运营商
- 接口：https://ip.taobao.com/outGetIpInfo?accessKey=alibaba-inc&ip=要查询的ip地址
- 每一行变成这种形式保存到文件
- 格式：ip,中国,湖南,电信
"""
# 要查询的ip
# ip_address = "172.8.133.220"
ip_address = input("输入你要查询的ip：")

# 请求接口获取IP地址信息
url = f"https://ip.taobao.com/outGetIpInfo?accessKey=alibaba-inc&ip={ip_address}"
response = requests.get(url)
data = response.json()

# 提取省份和运营商信息
province = data["data"]["city"]
isp = data["data"]["isp"]
country = data["data"]["country"]

# 将结果保存到文件
with open("ip_info.csv", "a", encoding="utf-8") as file:
    writer = csv.writer(file)
    writer.writerow([ip_address, country, province, isp])

